﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using System.Web.SessionState;

public partial class DataBaseManage_AddType : AdminBasePage
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string layerid = Request["layerid"];
            PopedomValidate(layerid, QUERY);
            this.headers.Text = new ColumnBll().LayeridSysColumnNaiv(Layerid);//系统
            string url = "type_add.aspx?tpid=" + Request.Params["tpid"] + "&layerid=" + Request.Params["layerid"];
            this.headers.Text += "<img src='../images/subnav_dot.gif' width='9' height='9' />&nbsp;&nbsp;<a href='" + url + "'>栏目类型详细信息</a>&nbsp;&nbsp";
            string str = Request["tpid"];
            if (str != null && str != "")
            {
                try
                {
                    DictionaryManageBLL dmb = new DictionaryManageBLL();
                    TypeManageBll tmb = new TypeManageBll();
                    TypeManageModel tmm = tmb.selectType(int.Parse(str));
                    txtTypname.Text = tmm.Ctname;
                    txtTemp.Text = tmm.Backlink;
                    txtexplain.Text = tmm.Explain;
                    /*
                     * 代码修改
                     * 修改日期：2012-01-15
                     * 修改人员：刘扬
                     * 修改原因：该变量仅为当前页面使用，故不需要使用Session进行保存，改为使用ViewState进行保存
                     * */
                    //Session["Name"]=tmm.Ctname;
                    ViewState["Name"] = tmm.Ctname;
                }
                catch (Exception ex)
                {

                }
            }
            
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string layerid = Request["layerid"];
        string str = Request["tpid"];
        if (str == null || str == "")
        {
            if (JudgmentRepeat() == 0)
            {
                insert();
            }
            else
            {
                Response.Write("<script>alert('名称重复!请重新输入!')</script>");
            }
        }
        else
        {
            if (this.txtTypname.Text.ToString().Trim() == ViewState["Name"].ToString())
            {
                TypeManageBll gttype = new TypeManageBll();
                TypeManageModel tmm = new TypeManageModel();
                tmm.Columntypeid = int.Parse(str);
                tmm.Ctname = this.txtTypname.Text.ToString().Trim();
                tmm.Backlink = this.txtTemp.Text.ToString().Trim();
                tmm.Explain = this.txtexplain.Text.ToString().Trim();
                int ts = gttype.updateType(tmm);
                if (ts > 0)
                {
                    string logcontent;
                    logcontent = "修改栏目类型--" + ViewState["Name"].ToString() + "修改为--" + this.txtTypname.Text.ToString();
                    this.insertLog(logcontent);
                    Response.Redirect("type_list.aspx?tpid=" + str + "&layerid=" + Request.Params["layerid"]);
                    return;
                }
            }
            else
            {
                if (JudgmentRepeat() == 0)
                {
                    TypeManageBll gttype = new TypeManageBll();
                    TypeManageModel tmm = new TypeManageModel();
                    tmm.Columntypeid = int.Parse(str);
                    tmm.Ctname = this.txtTypname.Text.ToString().Trim();
                    tmm.Backlink = this.txtTemp.Text.ToString().Trim();
                    tmm.Explain = this.txtexplain.Text.ToString().Trim();
                    int ts = gttype.updateType(tmm);
                    if (ts > 0)
                    {
                        string logcontent;
                        logcontent = "修改栏目类型--" + ViewState["Name"].ToString() + "修改为--" + this.txtTypname.Text.ToString();
                        this.insertLog(logcontent);
                        Response.Redirect("type_list.aspx?tpid=" + str + "&layerid=" + Request.Params["layerid"]);
                        return;
                    }
                }
                else
                {
                    Response.Write("<script>alert('名称重复!请重新输入!')</script>");
                }
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {

        Response.Redirect("type_list.aspx?layerid=" + Request.Params["layerid"]);
        return;
    }

    private void insert()
    {
        string user = GetSessionUsersModel().Aid.ToString();
        DateTime dt = DateTime.Now;//.ToString("yyyyMMddhhmmss");
        string str = Request["tpid"];
        TypeManageBll gttype = new TypeManageBll();
        TypeManageModel gttypeinfo = new TypeManageModel();
        gttypeinfo.Ctname = this.txtTypname.Text.ToString().Trim();
        gttypeinfo.Backlink = this.txtTemp.Text.ToString().Trim();
        gttypeinfo.Classname = this.txtTypname.Text.ToString().Trim();
        gttypeinfo.Addmanid = user;
        gttypeinfo.Explain = this.txtexplain.Text.ToString().Trim();
        gttypeinfo.Addtime=dt; 
        int ts = gttype.Add(gttypeinfo);
        if (ts > 0)
        {
            string logcontent;
            logcontent = "添加栏目类型--"+this.txtTypname.Text.ToString();
            this.insertLog(logcontent);
            Response.Redirect("type_list.aspx?tpid=" + str + "&layerid=" + Request.Params["layerid"]);
            return;
        }
        else
        {
        }
    }
    private int JudgmentRepeat()
    {
        string strWhere = this.txtTypname.Text.ToString().Trim();
        TypeManageBll tmb = new TypeManageBll();
        DataSet ds = tmb.GetList(" CTNAME = '"+strWhere+"'");
        if (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
}
